package com.friendlyarm.AndroidSDK;


import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.aitech.hardwareconctrl.MainActivity;
import com.friendlyarm.AndroidSDK.EngineParameter;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidParameterException;
import java.util.Timer;
import java.util.TimerTask;

import android_serialport_api.SerialPort;

public class SomeUtils {
    private static final String TAG = "airobot";
    public static Timer timer;// 创建舵机定时器对象
    public static TimerTask mTimerTask;

    public static Timer smiletimer;// 创建舵机定时器对象
    public static TimerTask mSmileTimerTask;
    public static Timer kisstimer;// 创建舵机定时器对象
    public static TimerTask mKissTimerTask;
    public static Timer openmouthtimer;// 创建舵机定时器对象
    public static TimerTask mopenmouthTimerTask;
    public static Timer sadnesstimer;// 创建舵机定时器对象
    public static TimerTask mSadnessTimerTask;
    public static Timer clowntimer;// 创建舵机定时器对象
    public static TimerTask mClownTimerTask;
    public static String devName = "/dev/ttyAMA3";//打开16路舵机控制板的串口 ttyAMA3
    public  static int  devfd = -1;
    public  static int  devfd4 = -1;
    public  static int  devfd3 = -1;
    public static String devName4 = "/dev/ttyAMA4"; ///dev/ttyAMA4";
    public static int speed4 = 38400;//baud波特率参数
    public static int speed = 9600;//baud波特率参数 //9600
    public static int dataBits = 8;
    public static int stopBits = 1;
    public static Boolean mouthFlag;

    public static SerialPort mSerialPort;
    public  static OutputStream mOutputStream;
    public  static InputStream mInputStream;

    public  static SerialPort mSerialPort0;
    public  static OutputStream mOutputStream0;
    public  static String   mBufferStart = "start";
    public  static InputStream mInputStream0;

    public static Timer readSerialTimer;
    public static TimerTask readSerialTimerTask;

    private static  String port = "/dev/ttySAC0";
    private static  int baudrate = 9600;

    public static SerialPort mSerialPort2;
    public  static OutputStream mOutputStream2;
    public  static InputStream mInputStream2;
    public static Timer readSerial2Timer;

    public static Boolean ActionDoing = false;
    /**
     * Serial Port 串口通信的接口说明 openSerialPort打开串口通信 初始化串口通信接口
     */

    public static Timer initRobotTimer;// 创建舵机定时器对象
    public static TimerTask initRobotTimertask;

    public static void initSerialPort()
    {
        // 打开
        //  public static String devName = "/dev/ttySAC3  ttySAC4" ttyAMA3
        try {
            mSerialPort = new SerialPort(new File("/dev/ttySAC4"), 9600, 0);
            // mSerialPort = mApplication.getSerialPort();
            mOutputStream = mSerialPort.getOutputStream();
            mInputStream = mSerialPort.getInputStream();
            Log.e(TAG, "----  initSerialPort ttySAC4 = " + mSerialPort+"  mOutputStream = "+mOutputStream +"  mInputStream = " + mInputStream);
            //receiveThread();
        } catch (SecurityException e) {
            Log.e(TAG, "----  initSerialPort  打开失败 0");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(TAG, "---- initSerialPort  打开失败 1");
            e.printStackTrace();
        }

        // 打开
        //  public static String devName = "/dev/ttySAC3  ttySAC4" ttyAMA3
        try {
            mSerialPort2 = new SerialPort(new File("/dev/ttySAC2"), 9600, 0);
            // mSerialPort = mApplication.getSerialPort();
            mOutputStream2 = mSerialPort2.getOutputStream();
            mInputStream2 = mSerialPort2.getInputStream();
            initRobot();
            Log.e(TAG, "----  mSerialPort ttySAC2 = " + mSerialPort2+"  mOutputStream2 = "+mOutputStream2 +"  mInputStream2 = " + mInputStream2);
            //receiveThread();
        } catch (SecurityException e) {
            Log.e(TAG, "----  initSerialPort mSerialPort2 打开失败 0");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(TAG, "---- initSerialPort mSerialPort2 打开失败 1");
            e.printStackTrace();
        }
    }



    public static Boolean opensuss(){
        if(devfd >= 0){
            return  true;
        }else{
            return  false;
        }
    }

    public static void startTimer(){
        if (timer == null) {
            timer = new Timer();
        }
        if (mTimerTask == null) {
            mTimerTask = new TimerTask() {
                int count = 0;
                int count1 = 0;
                boolean cycleEYEFlag = true;
                //boolean neckFlag = true;
                @Override
                public void run() {
                    count++;
                    count1++;
                    if (count == 1) {
                        // 控制 眼睛左眼皮闭合
                        writeTo(EngineParameter.data21);// 速度
                        writeTo(EngineParameter.data26);// 闭合
                        // 控制 眼睛右眼皮闭合
                        writeTo(EngineParameter.data41);// 速度
                        writeTo(EngineParameter.data46);// 闭合
                        Log.e(TAG, "----1闭眼睛" + count);
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        // 控制 眼睛左眼皮睁开
                        writeTo(EngineParameter.data21);// 速度
                        writeTo(EngineParameter.data25);// 睁开
                        // 控制 眼睛右眼皮睁开
                        writeTo(EngineParameter.data41);// 速度
                        writeTo(EngineParameter.data45);// 睁开
                        Log.e(TAG, "----2睁眼睛" + count);
                    } else if (count == 2) {
                        if (cycleEYEFlag) {
                            // 控制 眼睛两个眼珠左转
                            writeTo(EngineParameter.data60);// 速度
                            writeTo(EngineParameter.data66);// 左转data523
                            Log.e(TAG, "----3眼珠左转" + count);
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            // 控制 眼睛两个眼珠正位
                            writeTo(EngineParameter.data60);// 速度
                            writeTo(EngineParameter.data64);// 正位522
                            cycleEYEFlag = false;
                            Log.e(TAG, "----4眼珠正位" + count);
                        }
                    }else if(count == 3) {
                        if (!cycleEYEFlag) {
                            // 控制 眼睛两个眼珠右转
                            writeTo(EngineParameter.data60);// 速度
                            writeTo(EngineParameter.data65);// 右转
                            Log.e(TAG, "----5眼珠右转" + count);
                            // 控制 脸部左侧左转
                            writeTo(EngineParameter.data120);// 速度
                            writeTo(EngineParameter.data129);// 左转9°
                            // 控制脸部右侧右转
                            writeTo(EngineParameter.data130);// 速度
                            writeTo(EngineParameter.data139);// 右转9°
                            Log.e(TAG, "----6脸部左右上拉" + count);
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            // 控制 眼睛两个眼珠正位
                            writeTo(EngineParameter.data60);// 速度
                            writeTo(EngineParameter.data64);// 正位
                            cycleEYEFlag = true;
                            Log.e(TAG, "----7眼珠正位" + count);
                            // 控制 脸部左侧正位
                            writeTo(EngineParameter.data120);// 速度
                            writeTo(EngineParameter.data1208);// 正位
                            // 控制 脸部右侧正位
                            writeTo(EngineParameter.data130);// 速度
                            writeTo(EngineParameter.data1308);// 正位
                            Log.e(TAG, "----8脸部左右回正" + count);
                        }
                    }
                    else if (count == 4) {
                        count = 0;
                    }
                }
            };
            timer.schedule(mTimerTask, 2000, 1500);
        }
    }

    public static void speak(){
        stopTimer();
        startopenmouthTimer();
    }



    public static void stopspeak(){
        stopTimer();
        destroyopenmouthTimer();
    }

    public static void makeface(){
        SomeUtils.stopTimer();
        SomeUtils.startClownTimer();
    }
    public static void makesadness(){
        stopTimer();
        startSadnessTimer();
    }
    public static void smile(){
        stopTimer();
        startSmileTimer();
    }

    public static void kiss(){
        stopTimer();
        startKissTimer();
    }
    public static void eyesrotate(){
        stopTimer();
        startTimer();
    }
    public static void writCommonDate(int location,int angle,int speed){
        stopTimer();
        int locations=0x00;
        int mSpeed=0x05;
        if(location==1){
            //一号舵机 左眼皮
            locations=0x02;
            if(angle<90||angle>126){
                angle=126;
            }
        }else if(location==2){
            //二号舵机 右眼皮
            locations=0x04;
            if(angle<54||angle>90){
                angle=54;
            }
        }else if(location==3){
            //三号舵机 眼睛
            locations=0x06;
            //72 90 108
            if(angle<72||angle>108){
                angle=90;
            }
        }else if(location==4){
            //四号舵机 左脸舵机
            locations=0x0c;
            if(angle<58||angle>140){
                angle=90;
            }
        }else if(location==5){
            //五号舵机 右脸舵机
            locations=0x0d;
            if(angle<45||angle>120){
                angle=90;
            }
        }else if(location==6){
            //六号舵机 嘴巴
            locations=0x00;
            if(angle<85||angle>153){
                angle=85;
            }
        } else if(location==7){
            //七号舵机 脖子
            locations=0x08;
            if(angle<63||angle>117){
                angle=90;
            }
        }
        else{
            locations=0x02;
            angle=126;
        }

        if(speed==5){
            mSpeed=0x05;
        }else if(speed==10){
            mSpeed=0x10;
        }else if(speed==15){
            mSpeed=0x14;
        }else{
            mSpeed=0x05;
        }
        int tempangle=angle*11+500;
        Log.e(TAG, "---- writCommonDate:" + tempangle+" angle:"+angle+" locations: "+locations);
        int[] angles={tempangle};
        byte[] intToBytes=intToBytes(angles,0);
        byte[] speedS = {(byte) 0xFF, 0x01, (byte) locations, (byte) mSpeed, 0x00};
        byte[] date = { (byte) 0xFF, 0x02, (byte) locations,intToBytes[0],intToBytes[1]};
        SomeUtils.writeTo(speedS);
        //   writeComData(speedS);
        //   writeComData(date);
        SomeUtils.writeTo(date);
        //    return    date;
    }

    public static void Openmouth() {
        // SomeUtils.stopTimer();
        stopTimer();
        // 控制 眼睛左眼皮闭合
        Log.e(TAG, "----    张嘴 1");
        SomeUtils.writeTo(EngineParameter.data03);// 速度
        SomeUtils.writeTo(EngineParameter.data031);// 闭合 原始数据//data26
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.e(TAG, "----  张嘴 2");
        SomeUtils.writeTo(EngineParameter.data03);// 速度
        SomeUtils.writeTo(EngineParameter.data09);//
    }


    public static byte[] intToBytes(int[] src,int offset)
    {
        byte[] values = new byte[src.length*4];
        for(int i = 0; i < src.length; i++ )
        {
            values[offset+3] = (byte)((src[i] >> 24) & 0xFF);
            values[offset+2] = (byte)((src[i] >> 16) & 0xFF);
            values[offset+1] = (byte)((src[i]  >> 8) & 0xFF);
            values[offset] = (byte)(src[i]  & 0xFF);
            offset+=4;
        }
        return values;
    }

    public static void blinkeyes(){
        stopTimer();
        Log.e(TAG, "----   眨眼睛 ");
        writeTo(EngineParameter.data21);// 速度
        writeTo(EngineParameter.data26);// 闭合
        // 控制 眼睛右眼皮闭合
        writeTo(EngineParameter.data41);// 速度
        writeTo(EngineParameter.data46);// 闭合
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 控制 眼睛左眼皮睁开
        writeTo(EngineParameter.data21);// 速度
        writeTo(EngineParameter.data25);// 睁开
        // 控制 眼睛右眼皮睁开
        writeTo(EngineParameter.data41);// 速度
        writeTo(EngineParameter.data45);// 睁开
    }

    public static void blinklefteyes(){
        // 控制 眼睛左眼皮闭合
        stopTimer();
        Log.e(TAG, "---- xjy 20190621 眨左眼 ");
        writeTo(EngineParameter.data21);// 速度
        writeTo(EngineParameter.data26);// 闭合 90
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 控制 眼睛左眼皮睁开
        writeTo(EngineParameter.data21);// 速度
        writeTo(EngineParameter.data25);// 睁开

    }

    public static void blinkrighteyes(){
        stopTimer();
        Log.e(TAG, "---- xjy 20190621  眨右眼 ");
        writeTo(EngineParameter.data41);// 速度
        writeTo(EngineParameter.data46);// 闭合
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 控制 眼睛右眼皮睁开
        writeTo(EngineParameter.data41);// 速度
        writeTo(EngineParameter.data45);// 睁开
    }

    public static void turnNeck(){
        stopTimer();
        writeTo(EngineParameter.data80);// 速度
        writeTo(EngineParameter.data811);// 左转18°
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        writeTo(EngineParameter.data80);// 速度
        writeTo(EngineParameter.data88);// 正位72
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        writeTo(EngineParameter.data80);// 速度
        writeTo(EngineParameter.data85);// 右转18°

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        writeTo(EngineParameter.data80);// 速度
        writeTo(EngineParameter.data88);// 正位72
    }


    public static void stopTimer() {

        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (mTimerTask != null) {
            mTimerTask.cancel();
            mTimerTask = null;
        }
    }
    private static synchronized void destroySmileTimer() {
        Log.e(TAG, "---- destroyMyTimer : smiletimer = " + smiletimer + "   mSmileTimerTask=" + mSmileTimerTask);
        if (mSmileTimerTask != null) {
            mSmileTimerTask.cancel();
            mSmileTimerTask = null;
        }
        if (smiletimer != null) {
            smiletimer.cancel();
            smiletimer = null;
        }
    }

    public static void startSmileTimer() {
        destroySmileTimer();
        initSmileTimer();
        Log.e(TAG, "---- startSmileTimer : smiletimer = "+ smiletimer +"   mSmileTimerTask=" + mSmileTimerTask);
        if((smiletimer != null)&&(mSmileTimerTask != null)){
            smiletimer.schedule(mSmileTimerTask, 100, 1500);
        }
    }
    public static void initSmileTimer() {


        if (smiletimer == null) {
            smiletimer = new Timer();
        }
        if (mSmileTimerTask == null) {
            mSmileTimerTask = new TimerTask() {
                int count = 0;
                int count1 = 0;
                Boolean step1;
                @Override
                public void run() {
                    count++ ;
                    Log.e(TAG, "----initSmileTimer ：count =" + count + "  count1 = " + count1);
                    if(count1 == 1 ){  // 2
                        count = 0;
                        count1 = 0;
                        writeTo(EngineParameter.data120); //
                        writeTo(EngineParameter.data1208);//
                        writeTo(EngineParameter.data130); //
                        writeTo(EngineParameter.data1308);//

                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data25);// 左眼睁大
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data45);// 右眼闭合
                        destroySmileTimer();
                        // startTimer();
                        return;
                    }
                    if(count == 1){
                        Log.e(TAG, "----count == 1: 角度 =" + 135 +  "速度"+ 10);

                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data24);// 左眼睁大
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data44);// 右眼闭合

                        writeTo(EngineParameter.data121);// 速度 5
                        writeTo(EngineParameter.data1214);
                        writeTo(EngineParameter.data131 );// 速度 5
                        writeTo(EngineParameter.data1302);

                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data26);// 左眼闭合
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data46);// 右眼闭合
                    }
                    if(count == 3) {
                        Log.e(TAG, "----count == 3: 角度 =" + 90+ "速度"+ 10);
                        writeTo(EngineParameter.data120); //
                        writeTo(EngineParameter.data1208);//
                        writeTo(EngineParameter.data130); //
                        writeTo(EngineParameter.data1308);//

                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data26);// 左眼正常
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data46);// 右眼正常
                    }
                    if(count == 4){
                        Log.e(TAG, "----count == 4: 角度 =" + 135 + "速度"+ 10);
                        writeTo(EngineParameter.data121 );// 速度
                        writeTo(EngineParameter.data1214 );// 角度 135°
                        writeTo(EngineParameter.data131);// 速度
                        writeTo(EngineParameter.data1302);// 角度 45°

                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data26);// 左眼闭合
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data46);// 右眼闭合
                        count = 0;
                        count1 ++;
                    }
                }
            };
        }
    }
    private static synchronized void destroyClownTimer() {
        Log.e(TAG, "---- destroyClownTimer : clowntimer = " + clowntimer + "   mClownTimerTask=" + mClownTimerTask);
        if (mClownTimerTask != null) {
            mClownTimerTask.cancel();
            mClownTimerTask = null;
        }
        if (clowntimer != null) {
            clowntimer.cancel();
            clowntimer = null;
        }
    }
    public static void startClownTimer() {
        destroyClownTimer();
        initClownTimer();
        Log.e(TAG, "---- startClownTimer : clowntimer = "+ clowntimer +"   mClownTimerTask=" + mClownTimerTask);
        if((clowntimer != null)&&(mClownTimerTask != null)){
            clowntimer.schedule(mClownTimerTask, 100, 1500);
        }
    }
    public static void initClownTimer() {

        if (clowntimer == null) {
            clowntimer = new Timer();
        }
        if (mClownTimerTask == null) {
            mClownTimerTask = new TimerTask() {
                int count = 0;
                int count1 = 0;
                @Override
                public void run() {
                    count++;
                    Log.e(TAG, "----initClownTimer =" + count +"  count1 =" + count1);

                    if(count1 == 1){
                        destroyClownTimer();
                        //    startTimer();
                        count = 0;
                        count1 = 0;
                    }
                    if (count == 1) {
                        writeTo(EngineParameter.data120);// 左脸速度
                        writeTo(EngineParameter.data1212);// 左脸上拉//135
                        writeTo(EngineParameter.data130);// 右脸速度
                        writeTo(EngineParameter.data1308);// 右脸水平 90

                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data26);// 左眼闭合
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data44);// 右眼睁大
                    }
                    if( count == 2){
                        writeTo(EngineParameter.data120);// 左脸速度
                        writeTo(EngineParameter.data1208);// 左脸水平
                        writeTo(EngineParameter.data130);// 右脸速度
                        writeTo(EngineParameter.data1304);// 右脸上拉

                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data24);// 左眼睁大
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data46);// 右眼闭合

                    }
                    if( count == 3){
                        writeTo(EngineParameter.data120);// 左脸速度
                        writeTo(EngineParameter.data1212);// 左脸上拉
                        writeTo(EngineParameter.data130);// 右脸速度
                        writeTo(EngineParameter.data1308);// 右脸水平

                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data24);// 左眼睁大
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data46);// 右眼闭合
                    }
                    if( count == 4){
                        writeTo(EngineParameter.data120);// 左脸速度
                        writeTo(EngineParameter.data1208);// 左脸水平
                        writeTo(EngineParameter.data130);// 右脸速度
                        writeTo(EngineParameter.data1304);// 右脸上拉

                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data26);// 左眼闭合
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data44);// 右眼睁大
                    }
                    if(count == 5){
                        writeTo(EngineParameter.data120);// 左脸速度
                        writeTo(EngineParameter.data1206);// 左脸水平
                        writeTo(EngineParameter.data130);// 右脸速度
                        writeTo(EngineParameter.data1312);// 右脸上拉

                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data24);// 左眼闭合
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data44);// 右眼睁大

                    }
                    if(count == 6){
                        writeTo(EngineParameter.data120);// 左脸速度
                        writeTo(EngineParameter.data1208);// 左脸水平
                        writeTo(EngineParameter.data130);// 右脸速度
                        writeTo(EngineParameter.data1308);// 右脸上拉

                        writeTo(EngineParameter.data20);// 左眼速度
                        writeTo(EngineParameter.data25);// 左眼闭合
                        writeTo(EngineParameter.data40);// 右眼速度
                        writeTo(EngineParameter.data45);// 右眼睁大

                        count = 0;
                        count1 ++;
                    }
                }
            };
        }
    }

    private static synchronized void destroySadnessTimer() {
        Log.e(TAG, "---- destroySadnessTimer : sadnesstimer = " + clowntimer + "   mSadnessTimerTask=" + mClownTimerTask);
        if (mSadnessTimerTask != null) {
            mSadnessTimerTask.cancel();
            mSadnessTimerTask = null;
        }
        if (sadnesstimer != null) {
            sadnesstimer.cancel();
            sadnesstimer = null;
        }

    }
    public static void startSadnessTimer() {
        destroySadnessTimer();
        initSadnessTimer();
        Log.e(TAG, "---- startSadnessTimer : sadnesstimer = "+ sadnesstimer +"   mSadnessTimerTask=" + mSadnessTimerTask);
        if((sadnesstimer != null)&&(mSadnessTimerTask != null)){
            sadnesstimer.schedule(mSadnessTimerTask, 100, 1500);
        }
    }
    public static void initSadnessTimer()  {

        if (sadnesstimer == null) {
            sadnesstimer = new Timer();
        }
        if (mSadnessTimerTask == null) {
            mSadnessTimerTask = new TimerTask() {
                int count = 0;
                int count1 = 0;
                @Override
                public void run() {
                    count++ ;
                    Log.e(TAG, "----startSadnessTimer count =" + count + "  count1 = "+ count1);
                    if(count1 == 2){
                        Log.e(TAG, "----startSadnessTimer  count1 =" + count1);
                        destroySadnessTimer();
                        //   startTimer();
                        count = 0;
                        count1 = 0;
                    }
                    if (count == 1) {
                        Log.e(TAG, "----startSadnessTimer count =" + count);
                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data26);// 左眼闭合
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data46);// 右眼闭合

                        writeTo(EngineParameter.data120); //
                        writeTo(EngineParameter.data1208);//
                        writeTo(EngineParameter.data130); //
                        writeTo(EngineParameter.data1308);//
                    }
                    if( count == 2){
                        Log.e(TAG, "----startSadnessTimer count =" + count);
                        writeTo(EngineParameter.data121);// 速度 5
                        writeTo(EngineParameter.data1206);
                        writeTo(EngineParameter.data131 );// 速度 5
                        writeTo(EngineParameter.data1310);
                    }
                    if( count == 4){
                        Log.e(TAG, "----startSadnessTimer count =" + count);
                        writeTo(EngineParameter.data120);// 速度 5
                        writeTo(EngineParameter.data1208);
                        writeTo(EngineParameter.data130 );// 速度 5
                        writeTo(EngineParameter.data1308);
                        count1++;
                    }
                }
            };
        }
    }


    public static synchronized void destroyopenmouthTimer() {
        Log.e(TAG, "---- destroyKissTimer : openmousetimer = " + openmouthtimer + "   mopenmouseTimerTask=" + mopenmouthTimerTask);
        if (mopenmouthTimerTask != null) {
            mopenmouthTimerTask.cancel();
            mopenmouthTimerTask = null;
        }
        if (openmouthtimer != null) {
            openmouthtimer.cancel();
            openmouthtimer = null;
        }
        writeTo(EngineParameter.data03);//
        writeTo(EngineParameter.data09);//
    }

    public static void startopenmouthTimer() {
        destroyopenmouthTimer();
        initopenmouthTimer();
        mouthFlag=true;
        Log.e(TAG, "---- startKissTimer : openmousetimer = "+ openmouthtimer +"   mopenmouseTimerTask=" + mopenmouthTimerTask);
        if((openmouthtimer != null)&&(mopenmouthTimerTask != null)){
            openmouthtimer.schedule(mopenmouthTimerTask, 100, 100);
        }
    }

    public static void initopenmouthTimer(){
        if (openmouthtimer == null) {
            openmouthtimer = new Timer();
        }
        if (mopenmouthTimerTask == null) {
            mopenmouthTimerTask = new TimerTask() {
                int count = 0;
                int count1 = 0;
                @Override
                public void run() {
                    count++ ;
                    Log.e(TAG, "----initopenmouseTimer count =" + count +"  count1="+count1);
                    if(count1 == 100){
                        Log.e(TAG, "----initopenmouseTimer  count1 =" + count1);
                        destroyopenmouthTimer();
                        count = 0;
                        count1 = 0;
                    }

                    if (count % 3 == 0 && mouthFlag) {
                        // 控制嘴闭合
                        writeTo(EngineParameter.data03);// 速度
                        writeTo(EngineParameter.data09);
                        Log.e(TAG, "----  initopenmouthTimer 张嘴 count："+count+" mouseFlag:" +mouthFlag+ " count1："+count1);
                        mouthFlag = false;
                        // Log.e(TAG, "闭嘴");
                    } else if (count % 3 == 0 && !mouthFlag) {
                        // 控制嘴张开
                        writeTo(EngineParameter.data03);// 速度
                        writeTo(EngineParameter.data10);
                        Log.e(TAG, "---- initopenmouthTimer 闭嘴 count："+count+" mouseFlag:"+mouthFlag+ " count1："+count1);
                        mouthFlag = true;
                        count1++;
                    }
                }
            };
        }
    }

    private static synchronized void destroyKissTimer() {
        Log.e(TAG, "---- destroyKissTimer : kisstimer = " + kisstimer + "   mKissTimerTask=" + mKissTimerTask);
        if (mKissTimerTask != null) {
            mKissTimerTask.cancel();
            mKissTimerTask = null;
        }
        if (kisstimer != null) {
            kisstimer.cancel();
            kisstimer = null;
        }

    }
    public static void startKissTimer() {
        destroyKissTimer();
        initKissTimer();
        Log.e(TAG, "---- startKissTimer : kisstimer = "+ kisstimer +"   mKissTimerTask=" + mKissTimerTask);
        if((kisstimer != null)&&(mKissTimerTask != null)){
            kisstimer.schedule(mKissTimerTask, 100, 1500);
        }
    }
    public static void initKissTimer()  {
        if (kisstimer == null) {
            kisstimer = new Timer();
        }
        if (mKissTimerTask == null) {
            mKissTimerTask = new TimerTask() {
                int count = 0;
                int count1 = 0;
                @Override
                public void run() {
                    count++ ;
                    Log.e(TAG, "----initKissTimer count =" + count +"  count1="+count1);
                    if(count1 == 2){
                        Log.e(TAG, "----initKissTimer  count1 =" + count1);
                        destroyKissTimer();
                        //    startTimer();
                        count = 0;
                        count1 = 0;
                    }

                    if (count == 1) {
                        Log.e(TAG, "----initKissTimer count =" + count);
                        writeTo(EngineParameter.data121);// 左脸速度
                        writeTo(EngineParameter.data1208);// 左脸水平
                        writeTo(EngineParameter.data131);// 右脸速度
                        writeTo(EngineParameter.data1308);// 右脸上拉
                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data26);// 左眼闭合
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data46);// 右眼睁大
                    }
                    if( count == 2){
                        Log.e(TAG, "----initKissTimer count =" + count);
                        writeTo(EngineParameter.data121);// 左脸速度
                        //writeTo(EngineParameter.data1205);//左脸水平
                        writeTo(EngineParameter.data12060);//左脸水平
                        writeTo(EngineParameter.data131); //右脸速度
                        //writeTo(EngineParameter.data1313); //右脸上拉
                        writeTo(EngineParameter.data13100); //右脸上拉
                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data24);// 左眼闭合
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data44);// 右眼闭合
                    }
                    if( count == 4){
                        Log.e(TAG, "----initKissTimer count =" + count);
                        writeTo(EngineParameter.data120);// 速度 5
                        writeTo(EngineParameter.data1208);
                        writeTo(EngineParameter.data130 );// 速度 5
                        writeTo(EngineParameter.data1308);
                        writeTo(EngineParameter.data21);// 左眼速度
                        writeTo(EngineParameter.data25);// 左眼闭合
                        writeTo(EngineParameter.data41);// 右眼速度
                        writeTo(EngineParameter.data45);// 右眼睁大
                        count1++;
                    }
                }
            };
        }
    }



    public static void writeTo(byte[] data)
    {
        try {
            if (mOutputStream != null) {
                mOutputStream.write(data);
            } else {
                //Log.e(TAG, "native open returns");
            }
        } catch (IOException e) {
            e.printStackTrace();
            //Log.e(TAG, "native open ");
        }
    }


    public static void writeTodev2(int[] date) {

        byte[] bytedate =  intArrayToByteArray(date);
        Log.i("zeng", " . writeTodev2 : bytedate :"+bytedate );
        try {
            if (mOutputStream2 != null) {
                mOutputStream2.write(bytedate);
            } else {
                //Log.e(TAG, "native open returns");
            }
        } catch (IOException e) {
            e.printStackTrace();
            //Log.e(TAG, "native open ");
        }
    }

    public static byte[] intArrayToByteArray(int[] a) {
        int len = a.length;
        Log.e(TAG, "-----1 intArrayToByteArray  len = " + len);
        byte[] msgPack = new byte[len + 1];
        Log.e(TAG, "-----2 intArrayToByteArray  msgPack = " + msgPack);
        byte crc;
        for(int i= 0;i<len;i++){
            msgPack[i]=intToByte(a[i]);
            //Log.e(TAG, "-----3 intArrayToByteArray  a["+i+"]=" + a[i]+ "   msgPack["+i+"]=" + msgPack[i]);
        }
        String rt=byte2HexStr(msgPack);
        Log.e(TAG, "-----4 intArrayToByteArray  rt ="+ rt);

        byte[] msgPack2 = new byte[len-2];
        for( int j =0 ;j<len-2 ;j++)
            msgPack2[j] = msgPack[j+2];

        int crct2 = FindCRC2(msgPack2);
        Log.e(TAG, "-----9 intArrayToByteArray  crct2 = " + crct2);
        msgPack[len] = intToByte(crct2) ;
        String rtp3=byte2HexStr(msgPack);
        Log.e(TAG, "-----10 intArrayToByteArray  rtp3 ="+ rtp3);
        //----------------------------------
        return  msgPack;
    }


    public static void readSerial(){

        readSerialport0();
        readSerialport2();
    }

    public static boolean readSerialport0(){


        Log.e(TAG, "-----1 readSerial  mSerialPort0 =  " + mSerialPort0  );
        try {
            if (mSerialPort0 != null) {
                mSerialPort0 = null;
                mSerialPort0.close();
            }
            Log.d(TAG, "----2 readSerial Open SerialPort " );
            File file = new File("/dev/ttySAC0");
            mSerialPort0 = new SerialPort(file, 9600, 0);
            mOutputStream0 = mSerialPort0.getOutputStream();
            mInputStream0 = mSerialPort0.getInputStream();
            Log.d(TAG, "----3 readSerial nmSerialPort0 = "+mSerialPort0 + "   mOutputStream0 =" +mOutputStream0 +"  mInputStream0="+mInputStream0 );
            readSerialTimer = new Timer();
            readSerialTimer.schedule(readSerialTimerTask() ,1000,500);
            Log.d(TAG, "----5 readSerial Open SerialPort " );
            return true;
        } catch (SecurityException e) {
            Log.d(TAG, "---- readSerial SecurityException");
        } catch (IOException e) {
            Log.d(TAG, "---- readSerial IOException");
        } catch (InvalidParameterException e) {
            Log.d(TAG, "---- readSerial InvalidParameterException");
        }
        return false;
    }

    public static boolean readSerialport2(){


//        Log.e(TAG, "-----1 readSerial  mSerialPort0 =  " + mSerialPort0  );
//        try {
//            if (mSerialPort2 != null) {
//                mSerialPort2 = null;
//                mSerialPort2.close();
//            }
//            Log.d(TAG, "----2 readSerial Open SerialPort " );
//            File file = new File("/dev/ttySAC2");
//            mSerialPort2 = new SerialPort(file, 9600, 0);
        mOutputStream2 = mSerialPort2.getOutputStream();
        mInputStream2 = mSerialPort2.getInputStream();
        Log.d(TAG, "----3 readSerialport2 nmSerialPort0 = "+mSerialPort2 + "   mOutputStream2 =" +mOutputStream2 +"  mInputStream2="+mInputStream2 );
        readSerial2Timer = new Timer();
        readSerial2Timer.schedule(readSerial2TimerTask() ,500,500);
        Log.d(TAG, "----5 readSerialport2 Open SerialPort " );
//            return true;
//        } catch (SecurityException e) {
//            Log.d(TAG, "---- readSerialport2 SecurityException");
//        } catch (IOException e) {
//            Log.d(TAG, "---- readSerialport2 IOException");
//        } catch (InvalidParameterException e) {
//            Log.d(TAG, "---- readSerialport2 InvalidParameterException");
//        }
        return false;
    }


    public static TimerTask readSerialTimerTask() {
        TimerTask readSerialTimerTask = new TimerTask() {
            @Override
            public void run() {
                int size;
                int startIndex = 0;
                int startIndexdiv2 = 0;
                int revlength = 0;
                int revtype = 0;
                int revcrc =0;
                String vbattary = "";
                mInputStream0 = mSerialPort0.getInputStream();
                String rev="";
                try {
                    byte[] buffer = new byte[64];  //[64]
                    Log.e(TAG, "---- 1-1 readSerialTimerTask  buffer.length = "+ buffer.length);
                    for (int i = 0; i < buffer.length; i++) {
                        buffer[i] = 0;
                    }
                    if (mSerialPort0 == null || mInputStream0 == null) {
                        Log.e(TAG, "---- 2 readSerialTimerTask mInputStream0 == null");
                        return;
                    }
                    Log.e(TAG, "---- 3  readSerialTimerTask mInputStream0.read(buffer)");

                    size = mInputStream0.read(buffer);
                    String res = "";
                    res = new String(buffer, 0, size);
                    Log.e(TAG, "---- 5 readSerialTimerTask res = " + res);
                    String res2 = byteToHex(buffer);
                    rev = byte2HexStr(buffer);
                    if(!rev.contains("AA 96 00 01")) {
                        Log.e(TAG, "-----6    readSerialTimerTask  size= " + size + "    res2 = " + res2 + "   rev = " + rev);

                    }else{
                        Log.e(TAG, "-----6    readSerialTimerTask  数据接收正确 size= " + size + "    res2 = " + res2 + "   rev = " + rev);

                    }
                    int vindex = rev.indexOf("AA");
                    Log.e(TAG, "---- 6-1 readSerialTimerTask vindex = " + vindex);
                    int ikey = 1;
                    Log.e(TAG, "----7 readSerialTimerTask  ikey = " + ikey);

                    for (int h = 0; h <ikey; h++) {
                        //----------------------------------------------
                        startIndex = search2w ( res2,"AA" ,startIndex );
                        Log.e(TAG, "---- 71  readSerialTimerTask   startIndex = " + startIndex );
                        startIndexdiv2 = startIndex/2;
                        Log.e(TAG, "---- 72 readSerialTimerTask :  startIndex = " + startIndex +"  startIndexdiv2 = " + startIndexdiv2);
                        if( (startIndexdiv2+1) <= size){
                            int rev1 = byteToInt(buffer[startIndexdiv2+0]);
                            int rev2 = byteToInt(buffer[startIndexdiv2+1]);
                        }else{
                            Log.e(TAG, "----1 数据不全 " );
                            return;
                        }
                        //packtype
                        if( (startIndex+3) <= size) {
                            int rev3 = byteToInt(buffer[startIndexdiv2 + 2]);
                            int rev4 = byteToInt(buffer[startIndexdiv2 + 3]);
                            revtype = (rev3 << 8) | rev4;
                            Log.e(TAG, "---- 9 readSerialTimerTask   rev3 =0x" + numToHex8(rev3) + "   rev4=0x" + numToHex8(rev4) + "   revtype=0x" + numToHex8(revtype));
                        }else{
                            Log.e(TAG, "----2 数据不全 " );
                            return;
                        }
                        //LENGTH
                        if((startIndex + 5) <= size) {
                            int rev5 = byteToInt(buffer[startIndexdiv2 + 4]);
                            int rev6 = byteToInt(buffer[startIndexdiv2 + 5]);
                            revlength = (rev5 << 8) | rev6;
                            Log.e(TAG, "---- 10 readSerialTimerTask   rev5=0x" + numToHex8(rev5)
                                    + "   rev6=0x" + numToHex8(rev6) + "   revlength=0x" + numToHex8(revlength));
                        }else{
                            Log.e(TAG, "----3 数据不全 " );
                            return;
                        }
                        //CRC8
                        int crcp = startIndexdiv2+6+revlength;
                        Log.e(TAG, "---- 11-1 CRC8  startIndexdiv2 =" + startIndexdiv2+"    size =" + size);
                        if((crcp+1 ) <= size) {
                            revcrc = byteToInt(buffer[crcp]);
                        }else{

                            return;
                        }
                        Log.e(TAG, "---- 11-3 readSerialTimerTask   revcrc =0x" + numToHex8(revcrc));
                        //---------------------------------------------
                        int renL4 = revlength +4 ;
                        Log.e(TAG, "----12-1   readSerialTimerTask crc8: renL4 = " + renL4);
                        byte[] crc = new byte[renL4];
                        for (int k = 0; k < renL4; k++) {
                            crc[k] = buffer[startIndexdiv2 + 2 + k];
                        }
                        String rev2 = byte2HexStr(crc);
                        Log.e(TAG, "----12-2  readSerialTimerTask crc8: rev2 " + rev2);
                        int crc1 = FindCRC2(crc);
                        Log.e(TAG, "----12-3   readSerialTimerTask crc8: crc1 " + crc1);

                        if (crc1 == revcrc) {
                            Log.e(TAG, "----13 readSerialTimerTask crc8: CRC OK ");
                        } else {
                            Log.e(TAG, "----14 readSerialTimerTask crc8: CRC Err : crc1 = " + numToHex8(crc1)
                                    + "  revcrc =" + numToHex8(revcrc));
                            return ;
                        }
                        //---------------------------------------------
                        switch (revtype) {
                            case 1: //battery
                                if((startIndexdiv2+10)<= size) {
                                    int batFull = byteToInt(buffer[startIndexdiv2 + 6]);
                                    int batLow = byteToInt(buffer[startIndexdiv2 + 7]);
                                    int batVol = byteToInt(buffer[startIndexdiv2 + 8]);
                                    int batPer = byteToInt(buffer[startIndexdiv2 + 9]);
                                    int batAla = byteToInt(buffer[startIndexdiv2 + 10]);
                                    Log.e(TAG, "----17  readSerialTimerTask  battery batFull =" + batFull
                                            + "  batLow=" + batLow + "   batVol=" + batVol + "  batPer=" + batPer + "  batAla=" + batAla);

                                }else{
                                    Log.e(TAG, "----5 数据不全 " );
                                }
                                break;
                            case 2: //ir
                                if((startIndexdiv2+6) <= size) {
                                    int irVal = byteToInt(buffer[startIndexdiv2 + 6]);
                                    Log.e(TAG, "----6 is ir type " );
                                }else{
                                    Log.e(TAG, "----6 数据不全 " );
                                }
                                break;
                            case 3: //touch
                                if((startIndexdiv2+6) <= size) {
                                    int touchVal = byteToInt(buffer[startIndexdiv2 + 6]);
                                    Log.e(TAG, "----19    readSerialTimerTask  touch 触摸响应 touchVal= 0x" + numToHex8(touchVal));
                                    //mUtils.speak(" 触摸响应："+numToHex8(touchVal),mSpeakingListener);
                                    if(numToHex8(touchVal).equals("08")||numToHex8(touchVal).equals("10")) {

                                        Log.e(TAG, "----7 触摸响应 " );

                                    }
                                }else{
                                    Log.e(TAG, "----7 数据不全 " );
                                }
                                break;
                            case 4: //gesture  High position Low position	brightness Identification
                                if((startIndexdiv2+9) <= size) {
                                    int gestureHpVal = byteToInt(buffer[startIndexdiv2 + 6]);
                                    int gestureLpVal = byteToInt(buffer[startIndexdiv2 + 7]);
                                    int gestureBrigVal = byteToInt(buffer[startIndexdiv2 + 8]);
                                    int gestureIdenVal = byteToInt(buffer[startIndexdiv2 + 9]);
                                    Log.e(TAG, "----20    readSerialTimerTask   gesture  gestureHpVal = " + gestureHpVal
                                            + "  gestureLpVal" + gestureLpVal + "  gestureBrigVal" + gestureBrigVal + "  gestureIdenVal=" + gestureIdenVal);
                                }else{
                                    Log.e(TAG, "----8 数据不全 " );
                                }
                                break;
                            case 5: //bow
                                Log.e(TAG, "----21  readSerialTimerTask  bow ");
                                break;
                            default:
                                Log.e(TAG, "----22  readSerialTimerTask  default ");
                        }
                    }
                } catch(IOException e){
                    e.printStackTrace();
                    Log.d(TAG, "----23  readSerialTimerTask IOException ");
                    return;
                }
                Log.e(TAG, "---- Action doing  rev:"+rev);
                if(rev.contains("AA 96 01 02 00 01 01 03")) {
                    //action do update
                    if(ActionDoing) {
                        Log.e(TAG, "---- Action doing  false");
                        ActionDoing=false;
                    }
                }
            }
        };
        return readSerialTimerTask;
    }


    public static TimerTask readSerial2TimerTask(){
        TimerTask readSerial2TimerTask = new TimerTask() {
            @Override
            public void run() {
                int size;
                int startIndex = 0;
                int startIndexdiv2 = 0;
                int revlength = 0;
                int revtype = 0;
                int revcrc =0;
                String vbattary = "";
                mInputStream2 = mSerialPort2.getInputStream();
                String rev="";
                try {
                    byte[] buffer = new byte[64];  //[64]
                    Log.e(TAG, "---- 1-1 readSerial2TimerTask  buffer.length = "+ buffer.length);
                    for (int i = 0; i < buffer.length; i++) {
                        buffer[i] = 0;
                    }
                    if (mSerialPort2 == null || mInputStream2 == null) {
                        Log.e(TAG, "---- 2 readSerial2TimerTask mInputStream0 == null");
                        return;
                    }
                    Log.e(TAG, "---- 3  readSerial2TimerTask mInputStream0.read(buffer)");

                    size = mInputStream2.read(buffer);
                    String res = "";
                    res = new String(buffer, 0, size);
                    Log.e(TAG, "---- 5 readSerial2TimerTask res = " + res);
                    String res2 = byteToHex(buffer);
                    rev = byte2HexStr(buffer);
                    if(!rev.contains("AA 96 00 01")) {
                        Log.e(TAG, "-----6    readSerial2TimerTask  size= " + size + "    res2 = " + res2 + "   rev = " + rev);

                    }else{
                        Log.e(TAG, "-----6    readSerial2TimerTask  数据接收正确 size= " + size + "    res2 = " + res2 + "   rev = " + rev);

                    }
                    int vindex = rev.indexOf("AA");
                    Log.e(TAG, "---- 6-1 readSerial2TimerTask vindex = " + vindex);
                    int ikey = 1;
                    Log.e(TAG, "----7 readSerial2TimerTask  ikey = " + ikey);

                    for (int h = 0; h <ikey; h++) {
                        //----------------------------------------------
                        startIndex = search2w ( res2,"AA" ,startIndex );
                        Log.e(TAG, "---- 71  readSerial2TimerTask   startIndex = " + startIndex );
                        startIndexdiv2 = startIndex/2;
                        Log.e(TAG, "---- 72 readSerial2TimerTask :  startIndex = " + startIndex +"  startIndexdiv2 = " + startIndexdiv2);
                        if( (startIndexdiv2+1) <= size){
                            int rev1 = byteToInt(buffer[startIndexdiv2+0]);
                            int rev2 = byteToInt(buffer[startIndexdiv2+1]);
                        }else{
                            Log.e(TAG, "----1 数据不全 " );
                            return;
                        }
                        //packtype
                        if( (startIndex+3) <= size) {
                            int rev3 = byteToInt(buffer[startIndexdiv2 + 2]);
                            int rev4 = byteToInt(buffer[startIndexdiv2 + 3]);
                            revtype = (rev3 << 8) | rev4;
                            Log.e(TAG, "---- 9 readSerial2TimerTask   rev3 =0x" + numToHex8(rev3) + "   rev4=0x" + numToHex8(rev4) + "   revtype=0x" + numToHex8(revtype));
                        }else{
                            Log.e(TAG, "----2 数据不全 " );
                            return;
                        }
                        //LENGTH
                        if((startIndex + 5) <= size) {
                            int rev5 = byteToInt(buffer[startIndexdiv2 + 4]);
                            int rev6 = byteToInt(buffer[startIndexdiv2 + 5]);
                            revlength = (rev5 << 8) | rev6;
                            Log.e(TAG, "---- 10 readSerial2TimerTask   rev5=0x" + numToHex8(rev5)
                                    + "   rev6=0x" + numToHex8(rev6) + "   revlength=0x" + numToHex8(revlength));
                        }else{
                            Log.e(TAG, "----3 数据不全 " );
                            return;
                        }
                        //CRC8
                        int crcp = startIndexdiv2+6+revlength;
                        Log.e(TAG, "---- 11-1 CRC8  readSerial2TimerTask =" + startIndexdiv2+"    size =" + size);
                        if((crcp+1 ) <= size) {
                            revcrc = byteToInt(buffer[crcp]);
                        }else{

                            return;
                        }
                        Log.e(TAG, "---- 11-3 readSerial2TimerTask   revcrc =0x" + numToHex8(revcrc));
                        //---------------------------------------------
                        int renL4 = revlength +4 ;
                        Log.e(TAG, "----12-1   readSerial2TimerTask crc8: renL4 = " + renL4);
                        byte[] crc = new byte[renL4];
                        for (int k = 0; k < renL4; k++) {
                            crc[k] = buffer[startIndexdiv2 + 2 + k];
                        }
                        String rev2 = byte2HexStr(crc);
                        Log.e(TAG, "----12-2  readSerial2TimerTask crc8: rev2 " + rev2);
                        int crc1 = FindCRC2(crc);
                        Log.e(TAG, "----12-3   readSerial2TimerTask crc8: crc1 " + crc1);

                        if (crc1 == revcrc) {
                            Log.e(TAG, "----13 readSerial2TimerTask crc8: CRC OK ");
                        } else {
                            Log.e(TAG, "----14 readSerial2TimerTask crc8: CRC Err : crc1 = " + numToHex8(crc1)
                                    + "  revcrc =" + numToHex8(revcrc));
                            return ;
                        }
                        //---------------------------------------------
                        switch (revtype) {
                            case 1: //battery
                                if((startIndexdiv2+10)<= size) {
                                    int batFull = byteToInt(buffer[startIndexdiv2 + 6]);
                                    int batLow = byteToInt(buffer[startIndexdiv2 + 7]);
                                    int batVol = byteToInt(buffer[startIndexdiv2 + 8]);
                                    int batPer = byteToInt(buffer[startIndexdiv2 + 9]);
                                    int batAla = byteToInt(buffer[startIndexdiv2 + 10]);
                                    Log.e(TAG, "----17  readSerial2TimerTask  battery batFull =" + batFull
                                            + "  batLow=" + batLow + "   batVol=" + batVol + "  batPer=" + batPer + "  batAla=" + batAla);

                                }else{
                                    Log.e(TAG, "----5 数据不全 " );
                                }
                                break;
                            case 2: //ir
                                if((startIndexdiv2+6) <= size) {
                                    int irVal = byteToInt(buffer[startIndexdiv2 + 6]);
                                    Log.e(TAG, "----6 is ir type " );
                                }else{
                                    Log.e(TAG, "----6 数据不全 " );
                                }
                                break;
                            case 3: //touch
                                if((startIndexdiv2+6) <= size) {
                                    int touchVal = byteToInt(buffer[startIndexdiv2 + 6]);
                                    Log.e(TAG, "----19    readSerial2TimerTask  touch 触摸响应 touchVal= 0x" + numToHex8(touchVal));
                                    //mUtils.speak(" 触摸响应："+numToHex8(touchVal),mSpeakingListener);
                                    if(numToHex8(touchVal).equals("08")||numToHex8(touchVal).equals("10")) {

                                        Log.e(TAG, "----7 触摸响应 " );

                                    }
                                }else{
                                    Log.e(TAG, "----7 数据不全 " );
                                }
                                break;
                            case 4: //gesture  High position Low position	brightness Identification
                                if((startIndexdiv2+9) <= size) {
                                    int gestureHpVal = byteToInt(buffer[startIndexdiv2 + 6]);
                                    int gestureLpVal = byteToInt(buffer[startIndexdiv2 + 7]);
                                    int gestureBrigVal = byteToInt(buffer[startIndexdiv2 + 8]);
                                    int gestureIdenVal = byteToInt(buffer[startIndexdiv2 + 9]);
                                    Log.e(TAG, "----20    readSerial2TimerTask   gesture  gestureHpVal = " + gestureHpVal
                                            + "  gestureLpVal" + gestureLpVal + "  gestureBrigVal" + gestureBrigVal + "  gestureIdenVal=" + gestureIdenVal);
                                }else{
                                    Log.e(TAG, "----8 数据不全 " );
                                }
                                break;
                            case 5: //bow
                                Log.e(TAG, "----21  readSerial2TimerTask  bow ");
                                break;
                            default:
                                Log.e(TAG, "----22  readSerial2TimerTask  default ");
                        }
                    }
                } catch(IOException e){
                    e.printStackTrace();
                    Log.d(TAG, "----23  readSerial2TimerTask IOException ");
                    return;
                }
                Log.e(TAG, "---- Action doing readSerial2TimerTask   AA 96 01 02 00 01 01 03  rev:"+rev);
                if(rev.contains("AA 96 01 02 00 01 01 03")) {
                    //action do update
                    Log.e(TAG, "---- Action doing readSerial2TimerTask false is actiom");
                    if(ActionDoing) {
                        Log.e(TAG, "---- Action doing readSerial2TimerTask false");
                        ActionDoing=false;
                    }
                }
            }
        };
        return readSerial2TimerTask;
    }

    public static void initRobot(){
        initRobotTimer();
        if((initRobotTimer != null)&&(initRobotTimertask != null)){

            Log.e(TAG, "----    initRobot");
            initRobotTimer.schedule(initRobotTimertask, 3*1000);
        }
    }

    public static void initRobotTimer()  {
        if (initRobotTimer == null) {
            initRobotTimer = new Timer();
        }
        if (initRobotTimertask == null) {
            initRobotTimertask = new TimerTask() {
                String  contentone ="没有自定义播报内容" ;
                @Override
                public void run() {
                    if (true) {
                        writeTodev2(EngineParameter.initRobot);
                        Log.e(TAG, "---- initRobotTimer startPlaybackTimer : playbacktimer schedule run ");

                    }
                }
            };
        }
    }

    public static String byteToHex(byte[] bytes){
        String strHex = "";
        StringBuilder sb = new StringBuilder("");
        //Log.e(TAG, "---- byteToHex : bytes.length = "+ bytes.length);
        for (int n = 0; n < bytes.length; n++) {
            strHex = Integer.toHexString(bytes[n] & 0xFF);
            // Log.e(TAG, "---- byteToHex : strHex = "+ strHex);
            sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示，位数不够，高位补0
        }
        return sb.toString().toUpperCase().trim();
    }

    public static String byte2HexStr(byte[] b)
    {
        String stmp="";
        StringBuilder sb = new StringBuilder("");
        try {
            for (int n=0;n<b.length;n++)
            {
                stmp = Integer.toHexString(b[n] & 0xFF);
                sb.append((stmp.length()==1)? "0"+stmp : stmp);
                sb.append(" ");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return sb.toString().toUpperCase().trim();
    }
    public static int FindCRC2(byte[] data){
        int icrc=0;
        for(int i=0;i<data.length; i++){
            icrc ^=data[i];
        }
        return icrc;
    }
    public static int byteToInt(byte b) {return b & 0xFF; }
    public static int search2w(String str,String strRes,int startIndx) {
        int index = 0;//指定字符的长度
        index = str.indexOf(strRes,startIndx);
        return index;
    }
    public static String numToHex8(int b) {
        return String.format("%02x", b);//2表示需要两个16进行数
    }


    public static byte intToByte(int x) {
        return (byte) x;
    }
    public static int getSpeed(String speed){
        int speedtemp= Integer.parseInt(speed);
        if(speedtemp>28||speedtemp<0){
            return  255;
        }else{
            return  speedtemp*9;
        }
    }

    public static int getAngle(String angle){
        int angletemp=Integer.parseInt(angle);
        if(angletemp>180||angletemp<0){
            return  255;
        }else{
            return  angletemp;
        }

    }
    public static String str2HexStr(String origin)
    {
        byte[] bytes = origin.getBytes();
        String hex = bytesToHexString(bytes);
        return hex;
    }
    private static String bytesToHexString(byte[] src)
    {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0)
        {
            return null;
        }
        for (int i = 0; i < src.length; i++)
        {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2)
            {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
}
